<div>
    <!--部门查询页面-->
    <div class="ui top fluid attach container segment">
        <div class="ui blue top attached label">部门查询和新增</div>
        <div class="ui grid">
            <div class="row">
                <div class="five wide column">
                    <div class="ui small input">
                        <span class="all-condition-title">部门编号：</span>
                        <input type="text" id="departCode">
                    </div>
                </div>
                <div class="five wide column">
                    <div class="ui small input">
                        <span class="all-condition-title">部门名称：</span>
                        <input type="text" id="departName">
                    </div>
                </div>
                <div class="six wide column">
                    <button class="ui twitter small button all-button-between-margin" id="depart-setting-save"> 添 加
                    </button>
                    <button class="ui twitter small button all-button-between-margin" id="depart-setting-query"> 查 询
                    </button>
                </div>
            </div>
        </div>
    </div>

    <div class="ui bottom fluid attach container segment">
        <div class="ui blue top attached label">部门查询结果</div>
        <table class="ui celled padded table" id="departSettingTable">
            <thead>
            <tr>
                <th style="width: 20%;">数据操作</th>
                <th style="width: 20%;">记录流水号</th>
                <th style="width: 20%;">部门编号</th>
                <th style="width: 20%;">部门名称</th>
                <th style="width: 20%;">创建时间</th>
            </tr>
            </thead>
            <tbody id="contract-manage-list">
            <tr>
                <td>
                    <span class="ui center aligned">您还没有查询呀！</span>
                </td>
            </tr>
            </tbody>
            <tfoot>
            <tr>
                <th colspan="5">
                    <div class="ui grid">
                        <div class="row">
                            <div class="ten wide column"></div>
                            <div class="six wide column">
                                <button class="ui tiny disabled twitter button" id="contract-pre-page"> 上 一 页</button>
                                <span id="currentPage"> 0 </span> / <span id="totalPage"> 0 </span>&nbsp;
                                共 <span id="totalElement"> 0 </span> 条
                                <button class="ui tiny disabled twitter button" id="contract-next-page"> 下 一 页</button>
                                <input type="number" style="width: 3.5em; height: 2em;" id="pageNumber">
                                <a href="#" id="jumpQuery"> 跳 转 </a>
                            </div>
                        </div>
                    </div>
                </th>
            </tr>
            </tfoot>
        </table>
    </div>
</div>

<script>
    {
        //声明页面所需局部变量
        let pageInfo = {
            currentPage: 0,
            totalPages: 0,
            totalElement: 0
        }

        //部门信息查询
        let queryContractList = (queryParams, status) => {
            showWaitPromptBox("", "");
            $.ajax({
                url: "queryDepartSettingInfo",
                type: 'post',
                // async: false,
                data: queryParams,
                beforeSend: (request) => {
                    if (constant.csrfToken && constant.csrfHeader) {
                        request.setRequestHeader(constant.csrfHeader, constant.csrfToken);
                    }
                },
                success: (data) => {
                    $("#contract-manage-list").html("");
                    if ('0' === data.statusCode) {
                        let queryList = data.resultList;
                        let total = data.resultMap.total;
                        pageInfo.totalElement = data.resultMap.total;
                        $("#totalElement").text(pageInfo.totalElement);
                        let page = data.resultMap.pages;
                        pageInfo.totalPages = data.resultMap.pages;
                        let innerTextContractManage = "";
                        for (let i = 0; i < queryList.length; i++) {
                            innerTextContractManage +=
                                `<tr>
                                <td>
                                    <a class="deleteInfo" href="javascript:;"> 删除 </a>
                                </td>
                                <td>
                                    ${queryList[i].assistInfoId}
                                </td>
                                <td>
                                    ${queryList[i].infoCode}
                                </td>
                                <td>
                                    ${queryList[i].infoName}
                                </td>
                                <td>
                                    ${queryList[i].createTime.replace("T", " ").substr(0, 19)}
                                </td>
                            </tr>`
                        }
                        closeWaitPromptBox();
                        $("#contract-manage-list").html(innerTextContractManage);
                        $("#currentPage").text(pageInfo.currentPage + 1);
                        if (pageInfo.totalElement === 0) {
                            $("#currentPage").text(0);
                            $("#contract-manage-list").html(
                                `<tr>
                                <td>
                                    <span class="ui center aligned">当前暂无数据！</span>
                                </td>
                            </tr>`
                            );
                        }
                        $("#totalPage").text(page);

                        //更新按钮状态
                        if (status && pageInfo.totalElement > 10) {
                            $("#contract-next-page").removeClass("disabled")
                        }
                        if (status && pageInfo.totalElement <= 10) {
                            $("#contract-pre-page").addClass("disabled");
                            $("#contract-next-page").addClass("disabled")
                        }
                    } else if ('-1' === data.statusCode) {
                        closeWaitPromptBox();
                        showErrorPromptBox("", 2);
                    }
                },
                error: () => {
                    closeWaitPromptBox();
                    showErrorPromptBox("系统繁忙, 请稍后访问", 2);
                }
            });
        };

        //列表查询按钮
        $("#depart-setting-query").click(() => {
            queryFunc();
        });

        //列表查询:主要为了列表自动更新
        let queryFunc = () => {
            $("#contract-pre-page").addClass("disabled");
            $("#contract-next-page").addClass("disabled")
            pageInfo.currentPage = 0;
            let queryParams = {
                typeData: "部门",
                currentPage: pageInfo.currentPage,
                pageSize: constant.pageSize
            }
            queryContractList(queryParams, true);
        }

        //部门信息列表上一页
        $("#contract-pre-page").click(() => {
            pageInfo.currentPage = pageInfo.currentPage - 1;
            let queryParams = {
                currentPage: pageInfo.currentPage,
                pageSize: constant.pageSize
            }
            queryContractList(queryParams, false);
            if (pageInfo.currentPage <= 0) {
                $("#contract-pre-page").addClass("disabled");
            }
            if (pageInfo.currentPage <= pageInfo.totalPages + 1) {
                $("#contract-next-page").removeClass("disabled");
            }
        });

        //部门信息列表下一页
        $("#contract-next-page").click(() => {
            pageInfo.currentPage = pageInfo.currentPage + 1;
            let queryParams = {
                currentPage: pageInfo.currentPage,
                pageSize: constant.pageSize
            }
            queryContractList(queryParams, false);
            if (pageInfo.currentPage > 0) {
                $("#contract-pre-page").removeClass("disabled");
            }
            if (pageInfo.currentPage >= pageInfo.totalPages - 1) {
                $("#contract-pre-page").removeClass("disabled");
                $("#contract-next-page").addClass("disabled");
            }
        });

        //跳转到指定的页数
        $("#jumpQuery").click(() => {
            let pageNumber = $("#pageNumber").val() - 1;
            if (pageNumber === pageInfo.currentPage) {
                $("#pageNumber").val("");
                return false;
            }
            if (pageNumber < 0 || pageNumber >= pageInfo.totalPages) {
                $("#pageNumber").val("");
                showErrorPromptBox("输入的页码数有误, 请重新输入", 2);
                return false;
            }
            let queryParams = {
                currentPage: pageNumber,
                pageSize: constant.pageSize
            }
            if (queryParams.currentPage !== pageInfo.currentPage) {
                queryContractList(queryParams, false);
                pageInfo.currentPage = pageNumber;
                $("#pageNumber").val("");
            }
            if (pageNumber <= 0) {
                $("#contract-pre-page").addClass("disabled");
                $("#contract-next-page").removeClass("disabled");
            } else if (pageNumber > pageInfo.totalPages) {
                $("#contract-pre-page").removeClass("disabled");
                $("#contract-next-page").addClass("disabled");
            } else {
                $("#contract-pre-page").removeClass("disabled");
                $("#contract-next-page").removeClass("disabled");
            }
        });

        //添加新的部门信息
        $("#depart-setting-save").click(() => {
            let departCode = $("#departCode").val().trim();
            let departName = $("#departName").val().trim();

            if ("" === departCode || "" === departName) {
                showErrorPromptBox("部门编号或部门名称为空", 2);
                return false;
            }
            let queryParams = {
                typeData: "部门",
                departCode: departCode,
                departName: departName
            }
            showWaitPromptBox("部门信息保存中", "");
            $.ajax({
                url: "saveDepartSettingInfo",
                type: 'post',
                data: queryParams,
                beforeSend: (request) => {
                    if (constant.csrfToken && constant.csrfHeader) {
                        request.setRequestHeader(constant.csrfHeader, constant.csrfToken);
                    }
                },
                success: (data) => {
                    if ('0' === data.statusCode) {
                        closeWaitPromptBox();
                        showSuccessPromptBox("部门信息添加成功", 1);
                        //延迟执行列表刷新
                        setTimeout(() => {
                            queryFunc();
                        }, 1200);
                    } else if ('-1' === data.statusCode) {
                        closeWaitPromptBox();
                        showErrorPromptBox("", 2);
                    }
                },
                error: () => {
                    closeWaitPromptBox();
                    showErrorPromptBox("系统繁忙, 请稍后访问", 2);
                }
            });
            $("#departCode").val("");
            $("#departName").val("");
        });

        //删除部门信息：动态生成的元素只能用$("").on("click", ".deleteInfo", (e) => {});
        $("#departSettingTable").on("click", ".deleteInfo", (e) => {
            $("#delete-list-modal").modal({
                onApprove: function () {
                    let departSettingId = e.currentTarget.parentElement.nextElementSibling.innerText.trim();
                    let queryParams = {
                        departSettingId: departSettingId
                    }
                    $.ajax({
                        url: "deleteDepartSettingInfo",
                        type: 'post',
                        data: queryParams,
                        beforeSend: (request) => {
                            if (constant.csrfToken && constant.csrfHeader) {
                                request.setRequestHeader(constant.csrfHeader, constant.csrfToken);
                            }
                        },
                        success: (data) => {
                            if ('0' === data.statusCode) {
                                closeWaitPromptBox();
                                showSuccessPromptBox("部门信息删除成功", 1);
                                //延迟执行列表刷新
                                setTimeout(() => {
                                    queryFunc();
                                }, 1200);
                            } else if ('-1' === data.statusCode) {
                                closeWaitPromptBox();
                                showErrorPromptBox("", 2);
                            }
                        },
                        error: () => {
                            closeWaitPromptBox();
                            showErrorPromptBox("系统繁忙, 请稍后访问", 2);
                        }
                    });
                    $("#delete-list-modal").modal("hide");
                },
                onDeny: function () {

                }
            }).modal('show');
        });
    }
</script>